﻿layui.use('table', function () {
    var table = layui.table;
    //方法级渲染
    table.render({
        elem: '#LAY_table_user'
        , url: '/Button/GetButtonStyleList',
        size:'lg'
        , cols: [[
            { checkbox: true },
            { field: 'buttonStyleId', title: 'ID', width: 80, sort: true },
            { field: 'buttonStyleName', title: '按钮样式名称', width: 120 },
            { field: 'style', title: '按钮样式', width: 180},
            { field: 'size', title: '按钮大小', width: 120 },
            { field: 'types', title: '按钮类型', width: 180, },
            { field: 'icon', title: '小图标', width: 120 },
            { field: 'preview', title: '样式预览', width: 200, templet: '#sytlePreview' },
        ]]
        , id: 'id'
        , page: false
        , height: 630
    });
    $(document).ready(function () {
        //$("td div").removeClass("height").addClass("height","40");
        $("div").removeAttr("layui-table-cell","");
    });
    //新增按钮
    $("div").delegate('#AddButton','click',function (e) {
        var menuId = $("#MenuId").val();
        if (menuId === "" || menuId === null) {
            layer.alert("请选择要添加的菜单");
            return;
        }
        layer.open({
            type: 2,
            title: "新增【" + $("#MenuName").val()+"】菜单按钮",
            area: ['500px', '550px'],
            fixed: false, //不固定
            maxmin: true,
            shade: 0, //不遮挡
            id: 'addButton',//防止重复弹出
            content: '/Button/AddButton?MenuId=' + menuId,
            success: function (layero, index) {
                //layer.full(index);  //打开时最大化
            }
        });
        e.stopPropagation();  // 组织冒泡（点击一次执行2次）
    });

    //编辑按钮
    $("div").delegate('#EditButton', 'click', function (e) {
        var menuId = $("#MenuId").val();
        var ButtonId = $('input:radio:checked').val();
        if (ButtonId === "" || ButtonId === null || ButtonId === undefined) {
            layer.alert("请选择要编辑的按钮");
            return;
        }
        layer.open({
            type: 2,
            title: "编辑【" + $("#MenuName").val() + "】菜单按钮",
            area: ['500px', '550px'],
            fixed: false, //不固定
            maxmin: true,
            shade: 0, //不遮挡
            id: 'editButton',//防止重复弹出
            content: '/Button/EditButton?ButtonId=' + ButtonId + "&MenuId=" + menuId,
            success: function (layero, index) {
                //layer.full(index);  //打开时最大化
            }
        });
        e.stopPropagation();  // 组织冒泡（点击一次执行2次）
    });
    //保存按钮权限设置
    $("div").delegate('#SaveButton','click',function () {
        obj = document.getElementsByName("roleName");
        var RoleId = getUrlParam("RoleId");
        buttonRole = [];
        for (k in obj) {
            if (obj[k].checked)
                buttonRole.push({ ButtonId: obj[k].id, MenuId: $("#MenuId").val(), RoleId: RoleId});
        }
        if (buttonRole.length === 0) {
            layer.alert("没有选中信息");
            return;
        }
        layer.confirm('你确定要保存设置？', {
            btn: ['确定', '取消'] //按钮
        }, function () {
            $.ajax({
                url: "/Button/SetButtonRoleMsg",
                type: 'post',
                data: { ButtonRole: buttonRole, MenuId: $("#MenuId").val(), RoleId: RoleId },
                dataType: "json",
                async: "false",
                success: function (data) {
                    if (data.code === 0) {
                        layer.msg("设置成功", {
                            time: 2000 //20s后自动关闭
                            , icon: 1
                        });
                        setTimeout('myrefresh()', 800); //隔一秒刷新
                    }
                    else {
                        layer.msg("设置失败", {
                            time: 2000 //20s后自动关闭
                            , icon: 5
                        });
                    }
                }
            })
        });
    });

    //新增按钮样式
    $("div").delegate('#AddButtonStyle','click',function () {
        layer.open({
            type: 2,
            title: "新建按钮样式",
            area: ['500px', '550px'],
            fixed: false, //不固定
            maxmin: true,
            shade: 0, //不遮挡
            id: 'addButton',//防止重复弹出
            content: '/Button/AddButtonStyle',
            success: function (layero, index) {
                //layer.full(index);  //打开时最大化
            }
        });
    });

    //编辑按钮样式
    $("div").delegate('#EditButtonStyle','click',function () {
        // 获取选中列数据
        var checkStatus = table.checkStatus('id');
        if (checkStatus.data.length === 0) {
            layer.alert("没有编辑信息");
            return;
        }
        layer.open({
            type: 2,
            title: "编辑按钮样式",
            area: ['500px', '550px'],
            fixed: false, //不固定
            maxmin: true,
            shade: 0, //不遮挡
            id: 'editButton',//防止重复弹出
            content: '/Button/EditButtonStyle?ButtonStyleId=' + checkStatus.data[0].buttonStyleId,  // 只选择第一条
            success: function (layero, index) {
                //layer.full(index);  //打开时最大化
            }
        });
    }); 
});